/**
 * 南昌市国土资源局信息
 * http://nc.jxgtt.gov.cn/Column.shtml?p5=2962&slandno=all&saddress=all&sman=all
 */

const utils = require("../../utils");
import { Base, Item } from "../base";

class TempClass {
    name: string;
    url: string;
}


class ncgtzyjxx_spider extends Base {
    firstPage: string;
    constructor() {
        super();
        this.name = "南昌国土资源局信息";
        this.encode = "utf-8";
        this.debug = true;
        this.timeout = 1000000;
        this.collection = "ncgtzyjxx";
    }
    getPageCount(): number {
        let content: string = this.spider.getSync("http://nc.jxgtt.gov.cn/Column.shtml?p5=2962&slandno=all&saddress=all&sman=all");
        let $: CheerioSelector = this.cheerio.load(content);
        let num = $(".contlistmd table").eq(-1).find("tr").eq(-1).find("select option").eq(-1).text().split("/")[1];
        let cnt = parseInt(num);
        // console.log($(".contlistmd table").eq(-1));
        this.firstPage = content;
        return cnt;
    }

    getItems(pageIndex: number): Item[] {
        let result: Item[] = [];
        let content: string;
        if (pageIndex == 0) {
            content = this.firstPage;
        } else {
            content = "http://nc.jxgtt.gov.cn/Column.shtml?p5=2962&slandno=all&slandno=all&saddress=all&saddress=all&sman=all&sman=all&p7="
                + (pageIndex + 1);
            content = this.spider.getSync(content);
        }
        let $: CheerioSelector = this.cheerio.load(content);
        let self = this;
        let sizes = $(".contlistmd table").eq(1).find("tr").length;//页面长度(第一个和最后一个为无用数据)
        // console.log(sizes);
        $(".contlistmd table").eq(1).find("tr").each(function (index, ele) {
            if (index != 0 && index != sizes - 1) {
                let item: Item = self.buildItem();
                let zdNumber: string = $(ele).find("td").eq(0).text().trim();//宗地编号
                // console.log($(ele).find("td").eq(0).text());
                (item as any)["宗地编号"] = zdNumber;
                let cjPrice: string = $(ele).find("td").eq(2).text().trim();//成交单价
                // console.log(cjPrice);
                (item as any)["成交单价"] = cjPrice;
                let comPeople: string = $(ele).find("td").eq(3).text().trim();//竞得人
                // console.log(comPeople);
                (item as any)["竞得人"] = comPeople;
                let zdUrl = "http://nc.jxgtt.gov.cn/" + $(ele).find("td").eq(0).children("a").attr("href");//具体信息
                if (zdUrl) {

                    self.processItems(zdUrl, zdNumber, item);
                }
                result.push(item);
            }
        });
        return result;
    }
    //获得各公司的具体信息
    private processItems(zdUrl: string, zdNumber: string, item: Item): void {
        var that = this;
        var content: string = that.spider.getSync(zdUrl);
        var $: CheerioSelector = that.cheerio.load(content);
        (item as any)["url"] = zdUrl;
        var a = $(".contlistmd ").length;
        console.log(zdUrl);
        console.log(content);
        /*$(".contlistmd table tbody tr td.list2 table tbody").each(function (index, ele) {
            let zdAddress = $(ele).find("tr").eq(1).text().trim();
            console.log(zdAddress);
            console.log("-----");
        });*/
    }
}
export = new ncgtzyjxx_spider();